package in.ac.dei.edrp.cms.daoimpl.studentregistration;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import in.ac.dei.edrp.cms.dao.studentregistration.CheckDuplicateStudentDAO;
import in.ac.dei.edrp.cms.domain.LoginInfoGetter;
import in.ac.dei.edrp.cms.domain.studentregistration.StudentInfoGetter;

/**
 * Interface for registered student duplicate check 
 * @author Ankit Jain
 * @date 02-07-2011
 * @version 1.0
 */
public class CheckDuplicateStudentDAOImpl extends SqlMapClientDaoSupport implements CheckDuplicateStudentDAO{
	
	private Logger loggerObject = Logger.getLogger(CheckDuplicateStudentDAOImpl.class);
	
	@SuppressWarnings("unchecked")
	public boolean findDuplicateStudent(StudentInfoGetter studentInfoGetter)throws Exception {
		boolean flag=false;
		List<StudentInfoGetter> duplicateStudentsPrograms=null;
		List<StudentInfoGetter> duplicateStudentList= new ArrayList<StudentInfoGetter>();
		try{
			// clean temp_duplicate_student table
			getSqlMapClientTemplate().delete("checkDuplicateStudent.cleanTempDuplicateStudentTable", studentInfoGetter);
			// get the all new student
			List<StudentInfoGetter> studentList = getSqlMapClientTemplate().queryForList("checkDuplicateStudent.getStudentsRecord", studentInfoGetter);
			System.out.println(studentList.size()+": New student");
			for(int i=0;i<studentList.size();i++){
				studentInfoGetter.setStudentId(studentList.get(i).getStudentId());
				studentInfoGetter.setFirstName(studentList.get(i).getFirstName());
				studentInfoGetter.setMiddleName(studentList.get(i).getMiddleName());
				studentInfoGetter.setLastName(studentList.get(i).getLastName());
				studentInfoGetter.setFatherFirstName(studentList.get(i).getFatherFirstName());
				studentInfoGetter.setFatherMiddleName(studentList.get(i).getFatherMiddleName());
				studentInfoGetter.setFatherLastName(studentList.get(i).getFatherLastName());
				studentInfoGetter.setMotherFirstName(studentList.get(i).getMotherFirstName());
				studentInfoGetter.setMotherMiddleName(studentList.get(i).getMotherMiddleName());
				studentInfoGetter.setMotherLastName(studentList.get(i).getMotherLastName());
				studentInfoGetter.setDateOfBirth(studentList.get(i).getDateOfBirth());
				studentInfoGetter.setCategory(studentList.get(i).getCategory());
				studentInfoGetter.setGender(studentList.get(i).getGender());
				// get the duplicate student
				List<StudentInfoGetter> listObject =getSqlMapClientTemplate().queryForList("checkDuplicateStudent.findDuplicateStudent", studentInfoGetter);
								
				if(listObject.size()>1){
					for(int m=0;m<listObject.size();m++){
						int check=0;
						if(duplicateStudentList.size()>0){
							for(int k=0;k<duplicateStudentList.size();k++){
								if(listObject.get(m).getStudentId().equals(duplicateStudentList.get(k).getStudentId())){
									check=check+1;
									break;								
								}
							}
							if(check>0){}
							else{
								duplicateStudentList.add(listObject.get(m));
							}
						}
						else{
							duplicateStudentList.add(listObject.get(m));
						}
					}
				}
			}// end of outer loop
			
			System.out.println("XXXXXXXX: "+duplicateStudentList.size());
			for(int z=0;z<duplicateStudentList.size();z++){
				System.out.println("Elements: "+duplicateStudentList.get(z).getStudentId());
			}
				
			// if find more then 1 student then student is duplicate.
			if(duplicateStudentList.size()>1){
				flag=true;
				StudentInfoGetter infoGetter=new StudentInfoGetter();
				 for (int j=0;j<duplicateStudentList.size();j++){
					 infoGetter.setStudentId(duplicateStudentList.get(j).getStudentId());
					// get the program details of the duplicate student.
					duplicateStudentsPrograms=getSqlMapClientTemplate().queryForList("checkDuplicateStudent.getDuplicateStudentsProgramInfo", studentInfoGetter);
					 
					 infoGetter.setProgramId(duplicateStudentsPrograms.get(0).getProgramId());
					 infoGetter.setBranchId(duplicateStudentsPrograms.get(0).getBranchId());
					 infoGetter.setSpecializationId(duplicateStudentsPrograms.get(0).getSpecializationId());
					 infoGetter.setSemesterCode(duplicateStudentsPrograms.get(0).getSemesterCode());
					 infoGetter.setStudentId(duplicateStudentList.get(j).getStudentId());
					 infoGetter.setRegistrationNumber(duplicateStudentList.get(j).getRegistrationNumber());
					 infoGetter.setFirstName(studentList.get(j).getFirstName());
					 infoGetter.setMiddleName(studentList.get(j).getMiddleName());
					 infoGetter.setLastName(studentList.get(j).getLastName());
					 infoGetter.setFatherFirstName(studentList.get(j).getFatherFirstName());
					 infoGetter.setFatherMiddleName(studentList.get(j).getFatherMiddleName());
					 infoGetter.setFatherLastName(studentList.get(j).getFatherLastName());
					 infoGetter.setMotherFirstName(studentList.get(j).getMotherFirstName());
					 infoGetter.setMotherMiddleName(studentList.get(j).getMotherMiddleName());
					 infoGetter.setMotherLastName(studentList.get(j).getMotherLastName());
					 infoGetter.setDateOfBirth(studentList.get(j).getDateOfBirth());
					 infoGetter.setCategory(studentList.get(j).getCategory());
					 infoGetter.setGender(studentList.get(j).getGender());
					 infoGetter.setUniversityId(studentInfoGetter.getUniversityId());
					 infoGetter.setCreatorId(studentInfoGetter.getCreatorId());
					 // insert duplicate student details into temp_duplicate_student table. 
					 getSqlMapClientTemplate().insert("checkDuplicateStudent.InsertDuplicateStudents", infoGetter);
				 }
			}
				
			
			
			StudentInfoGetter temp=(StudentInfoGetter)getSqlMapClientTemplate().queryForObject("checkDuplicateStudent.getDuplicateRecordExistFlag", studentInfoGetter);
			if(temp.getStatus().equals(null) || temp.getStatus()!=""){// flag is already set in system_values table
				if(flag){
					studentInfoGetter.setStatus("Y");
					getSqlMapClientTemplate().update("checkDuplicateStudent.updateDuplicateRecordExistFlag", studentInfoGetter);					
				}
				else{
					studentInfoGetter.setStatus("N");
					getSqlMapClientTemplate().update("checkDuplicateStudent.updateDuplicateRecordExistFlag", studentInfoGetter);
				}
			}
			else{
				if(flag){
					studentInfoGetter.setStatus("Y");					getSqlMapClientTemplate().insert("checkDuplicateStudent.setDuplicateRecordExistFlag", studentInfoGetter);
				}
				else{
					studentInfoGetter.setStatus("N");
					getSqlMapClientTemplate().insert("checkDuplicateStudent.setDuplicateRecordExistFlag", studentInfoGetter);
				}
			}
						
			loggerObject.info("Inside CheckDuplicateStudentDAOImpl");
		}
		catch (Exception e) {
			loggerObject.error("CheckDuplicateStudentDAOImpl "+ e);
		}
		return flag;
	}
	
	@SuppressWarnings("unchecked")
	public List<StudentInfoGetter> getDuplicateStudentList(StudentInfoGetter studentInfoGetter) throws Exception {
		List<StudentInfoGetter> studentInfoGetter1=null;		
		try{
			loggerObject.info("Inside getDuplicateStudentList");
			studentInfoGetter1 = getSqlMapClientTemplate().queryForList("checkDuplicateStudent.getDuplicateStudentList", studentInfoGetter);
		}
		catch (Exception e) {
			loggerObject.info("Inside getDuplicateStudentList: "+e);
		}
		return studentInfoGetter1;
	}
	
	
	public String getProcessStatus(StudentInfoGetter studentInfoGetter) throws Exception {
		StudentInfoGetter studentInfoGetter1=null;
		String status=null;
		try{
			loggerObject.info("Inside getDuplicateStudentList");
			studentInfoGetter1=(StudentInfoGetter)getSqlMapClientTemplate().queryForObject("checkDuplicateStudent.getDuplicateRecordExistFlag", studentInfoGetter);
			status=studentInfoGetter1.getStatus();
			System.out.println(status+": status");
		}
		catch (Exception e) {
			loggerObject.info("Inside getDuplicateStudentList: "+e);
		}
		return status;
	}
	

}
